package com.yoo.ons.function.me.viewmodel

import androidx.lifecycle.MutableLiveData
import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.yoo.ons.model.UserInfoModel
import com.yoo.ons.base.BaseVMViewModel
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.base.Request
import com.yoo.ons.model.PremiumModel
import com.yoo.ons.model.QueryBenefitsModel

/**
 * @Author andy
 * @Date 2024/5/28 20:24
 */
class MeViewModel:BaseVMViewModel() {

    val userInfoLiveData = MutableLiveData<UserInfoModel?>()
    val closeDialogLiveData = MutableLiveData<Boolean>()

    fun closeProfileDialog(isShow:Boolean){
        closeDialogLiveData.value = isShow
    }

    /**
     * 获取i like 数量
     */
    fun getILikeCount(listener: HttpRequestListener<Int>){
        request(listener,request=object :Request{
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.getUserLikeCount(map)?.requestObserver(
                    NetworkObserver<Int>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }

    /**
     * 查询权益剩余数量
     */
    fun getQueryBenefits(listener: HttpRequestListener<List<QueryBenefitsModel>>){
        request(listener,request=object :Request{
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postQueryBenefits(map)?.requestObserver(
                    NetworkObserver<List<QueryBenefitsModel>>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }

    /**
     * 查询会员页面信息
     */
    fun getPremiumSubscription(listener: HttpRequestListener<PremiumModel>){
        request(listener,request=object :Request{
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postPremiumSubscription(map)?.requestObserver(
                    NetworkObserver<PremiumModel>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }
    /**
     * 查询用户信息
     */
    fun getUserInfo(listener: HttpRequestListener<UserInfoModel>){
        request(listener,request=object :Request{
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postUserInfo(map)?.requestObserver(
                    NetworkObserver<UserInfoModel>().startRequest {
                    }.success {
                        userInfoLiveData.value = it
                        //listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }
}